CREATE OR REPLACE PACKAGE BODY pkg_msg_array
IS
   
   PROCEDURE p_add( message VARCHAR2 )
   IS
   BEGIN
      g_idx := g_idx + 1;
      g_msg_array( g_idx ) := SUBSTR( message, 1, 1000 );
   END p_add;
   
   PROCEDURE p_clear
   IS
   BEGIN
      g_msg_array.DELETE;
      g_idx := 0;
   END p_clear;
   
   PROCEDURE p_print
      ( how VARCHAR2 := 'HTML'
      , table_style VARCHAR2 DEFAULT NULL
      , table_header VARCHAR2 DEFAULT NULL
      , th_style VARCHAR2 DEFAULT NULL
      , td_style VARCHAR2 DEFAULT NULL )
   IS
   BEGIN
      IF g_idx > 0 THEN
         IF how = 'DBMS' THEN
            FOR i IN g_msg_array.FIRST..g_msg_array.LAST LOOP
               DBMS_OUTPUT.PUT_LINE( g_msg_array( i ) );
            END LOOP;
         ELSE
            htp.p( '<table '||table_style||'>' );     
            IF table_header IS NOT NULL THEN
               htp.p( '<tr><th '||th_style||'>'||table_header||'</th></tr>' );
            END IF;
            FOR i IN g_msg_array.FIRST..g_msg_array.LAST LOOP
               htp.p( '<tr><td '||td_style||'>'||g_msg_array( i )||'</td></tr>' );
            END LOOP;
            htp.p( '</table>' );
         END IF;      
      END IF;        
   END p_print; 

END pkg_msg_array;

/
